package cn.iocoder.yudao.module.travel.controller.admin.travel1;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.travel.controller.admin.travel1.vo.*;
import cn.iocoder.yudao.module.travel.dal.dataobject.travel1.TravelDO;
import cn.iocoder.yudao.module.travel.service.travel1.TravelService;

@Tag(name = "管理后台 - 用户基本信息")
@RestController
@RequestMapping("/travel/travel")
@Validated
public class TravelController {

    @Resource
    private TravelService travelService;

    @PostMapping("/create")
    @Operation(summary = "创建用户基本信息")
    @PreAuthorize("@ss.hasPermission('travel:travel:create')")
    public CommonResult<Long> createTravel(@Valid @RequestBody TravelSaveReqVO createReqVO) {
        return success(travelService.createTravel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户基本信息")
    @PreAuthorize("@ss.hasPermission('travel:travel:update')")
    public CommonResult<Boolean> updateTravel(@Valid @RequestBody TravelSaveReqVO updateReqVO) {
        travelService.updateTravel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('travel:travel:delete')")
    public CommonResult<Boolean> deleteTravel(@RequestParam("id") Long id) {
        travelService.deleteTravel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('travel:travel:query')")
    public CommonResult<TravelRespVO> getTravel(@RequestParam("id") Long id) {
        TravelDO travel = travelService.getTravel(id);
        return success(BeanUtils.toBean(travel, TravelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户基本信息分页")
    @PreAuthorize("@ss.hasPermission('travel:travel:query')")
    public CommonResult<PageResult<TravelRespVO>> getTravelPage(@Valid TravelPageReqVO pageReqVO) {
        PageResult<TravelDO> pageResult = travelService.getTravelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TravelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户基本信息 Excel")
    @PreAuthorize("@ss.hasPermission('travel:travel:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTravelExcel(@Valid TravelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TravelDO> list = travelService.getTravelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户基本信息.xls", "数据", TravelRespVO.class,
                        BeanUtils.toBean(list, TravelRespVO.class));
    }

}